package com.huike.business.service.impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.domain.vo.TbZd;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.enums.BusinessStatus;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商机跟进记录Service业务层处理
 *
 * @author wgl
 * @date 2021-04-28
 */
//新增商机跟进
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {
    @Autowired
    private TbBusinessTrackRecordMapper tbBusinessTrackRecordMapper;

    @Autowired
    private TbBusinessMapper tbBusinessMapper;

    // 商机跟进新增
    @Override
    public Integer add(BusinessTrackVo businessTrackVo) {
        //获得用户name
        String username = SecurityUtils.getUsername();
        //补全信息 创建一个商机跟进对象
        TbBusinessTrackRecord businessTrackRecord = new TbBusinessTrackRecord();
        businessTrackRecord.setBusinessId(businessTrackVo.getBusinessId()); //商机id
        businessTrackRecord.setTrackStatus(businessTrackVo.getTrackStatus());//状态
        businessTrackRecord.setNextTime(businessTrackVo.getNextTime());//下次跟进时间
        businessTrackRecord.setKeyItems(businessTrackVo.getKeyItems());//沟通重点
        businessTrackRecord.setRecord(businessTrackVo.getRecord()); //沟通纪要
        businessTrackRecord.setCreateBy(username);//跟进人
        businessTrackRecord.setCreateTime(DateUtils.getNowDate());//跟进时间
        businessTrackRecord.setType(businessTrackVo.getType());//0 正常跟进记录 1 退回公海
        businessTrackRecord.setFalseReason(businessTrackVo.getFalseReason());//退回公海原因
        //商机对象
        TbBusiness tbBusiness = new TbBusiness();
        tbBusiness.setId(businessTrackVo.getBusinessId());//id
        tbBusiness.setName(businessTrackVo.getName());//姓名
        tbBusiness.setPhone(businessTrackVo.getPhone());//手机
        tbBusiness.setOccupation(businessTrackVo.getOccupation());//职业
        tbBusiness.setEducation(businessTrackVo.getEducation());//学历
        tbBusiness.setProvinces(businessTrackVo.getProvinces());//地区
        tbBusiness.setCity(businessTrackVo.getCity());//市辖区
        tbBusiness.setAge(businessTrackVo.getAge());//年龄
        tbBusiness.setMajor(businessTrackVo.getMajor());//专业
        tbBusiness.setJob(businessTrackVo.getJob());//专业
        tbBusiness.setSalary(businessTrackVo.getSalary());//薪资
        tbBusiness.setQq(businessTrackVo.getQq());//qq
        tbBusiness.setSex(businessTrackVo.getSex());//性别
        tbBusiness.setExpectedSalary(businessTrackVo.getExpectedSalary());//期望薪资
        tbBusiness.setRemark(businessTrackVo.getRemark());//备注
        tbBusiness.setSubject(businessTrackVo.getSubject());//学科
        tbBusiness.setReasons(businessTrackVo.getReasons());//学习原因
        tbBusiness.setPlan(businessTrackVo.getPlan());//职业计划
        tbBusiness.setPlanTime(businessTrackVo.getPlanTime());//计划学习
        tbBusiness.setCourseId(businessTrackVo.getCourseId());//课程id
        tbBusiness.setOtherIntention(businessTrackVo.getOtherIntention());//其他意向
        tbBusiness.setChannel(String.valueOf(businessTrackVo.getChannelId()));//渠道id
        tbBusiness.setActivityId(businessTrackVo.getActivityId());//活动id

        tbBusinessMapper.updateTbBusiness(tbBusiness);
        return tbBusinessTrackRecordMapper.add(businessTrackRecord);
    }

    // 查询商机跟进表
    @Override
    public   List<TbBusinessTrackRecord> list(Long id) {
        //根据商机id查询商机跟进对象
        List<TbBusinessTrackRecord> list =tbBusinessTrackRecordMapper.list(id);
        //字典对象
        ArrayList<String> keys = new ArrayList<>();
        List<Map<String, String>> list2=tbBusinessTrackRecordMapper.zd();
        for (TbBusinessTrackRecord businessTrackRecord : list) {
            String keyItems = businessTrackRecord.getKeyItems();
        //拿到跟进里面数字
        String[] key = keyItems.split(",");
        for (String s : key) {
            //循环字典对象
            for (Map<String, String> map : list2) {
                //获得数字对应的label s1=1
                 String s1 = map.get("dictValue");
               if (s1.equals(s)){
                   String dictLabel = map.get("dictLabel");
                   //存储到集合
                   keys.add(dictLabel);
               }

            }
        }
            businessTrackRecord.setKeys(keys);

        }

        return list;
    }
}
